﻿<%@ Page Title="plantinfo" Language="C#" MasterPageFile="~/EasyUI.master" AutoEventWireup="true" CodeBehind="List.aspx.cs" Inherits="cebao.Web.plantinfo.List" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

    <script type="text/javascript">
        var selectNode;
       
        $(function () {
            InitTree();
        });      
        function InitTree() {
            $('#p').panel({
                //height: $(parent.document).find("#mainPanle").height() - 35 > 0 ? $(parent.document).find("#mainPanle").height() - 35 : 500, //高度
                height: '100%',
            });
            $('#tt').tree({                
                url: 'GetPlant.ashx', //请求数据的页面               
                width: '100%', //宽度
                //height: $(parent.document).find("#mainPanle").height() - 35 > 0 ? $(parent.document).find("#mainPanle").height() - 35 : 500, //高度
                lines:true,
                method: 'post',
                formatter: function (node) {
                    return node.bm + " " + node.text;
                },
                queryParams: { "action": "query" },
                onLoadSuccess: function (node, data) {
                    if (selectNode) {
                        $('#tt').tree('collapseAll');
                        var nodeNew = $('#tt').tree('find', selectNode.id);
                        $('#tt').tree('expandTo', nodeNew.target);
                        $('#tt').tree('expand', nodeNew.target);
                        $('#tt').tree('scrollTo', nodeNew.target);
                    }
                    else
                        $('#tt').tree('collapseAll');

                },
                onContextMenu: function (e, node) {
                    e.preventDefault();
                    // 查找节点
                    $('#tt').tree('select', node.target);
                    // 显示快捷菜单
                    $('#mm').menu('show', {
                        left: e.pageX,
                        top: e.pageY
                    });
                }
            });
        }
       
        function newNode() {             
            $('#fm').form('clear');           
            var node = $('#tt').tree('getSelected');
            if (node!=null)
            {
                $('#parentid').val(node.id);
                selectNode = node;
            }
            $('#dlg').dialog('open').dialog('setTitle', '添加植物');
            document.getElementById("test").value = "add";
        }        
        function editNode() {
            var row = $('#tt').tree('getSelected');
            if (row == null) {
                $.messager.alert("提示", "请选择要修改的数据！", "info");
            }
            if (row) {                
                $('#pid').val(row.id);
                $('#pname').val(row.text);
                $('#pbm').val(row.bm);
                selectNode = row;
                document.getElementById("test").value = "modify";                
                $('#dlg').dialog('open').dialog('setTitle', '修改植物');
            }
        }               
        function saveNode() {
            var test = document.getElementById("test").value;
            var f = $('#fm');
            if (test == "add") {
                f.form('submit', {
                    url: "GetPlant.ashx?action=" + test,
                    onSubmit: function () {
                        return $(this).form('validate');
                    },
                    success: function (result) {
                        if (result.indexOf("T") == 0) {
                            $('#dlg').dialog('close');
                            $.messager.alert("提示", "恭喜您，信息添加成功", "info");
                            $('#tt').tree('reload');
                        }
                        else if ((result.indexOf("F") == 0)) {
                            $.messager.alert("提示", "添加失败，请重新操作！", "info");
                            return;
                        }
                        else if ((result.indexOf("L") == 0)) {
                            $.messager.alert("提示", "长时间没有操作，请重新登录！", "info");
                            top.location = '/index.aspx';
                            return;
                        }
                    }
                });

            } else {               
                $('#fm').form('submit', {
                    url: "GetPlant.ashx?action=" + test,
                    onSubmit: function () {
                        return $(this).form('validate');
                    },
                    success: function (result) {
                        if (result.indexOf("T") == 0) {
                            $('#dlg').dialog('close');                            
                            $.messager.alert("提示", "恭喜您，信息修改成功", "info");
                            $('#tt').tree('reload');
                        }
                        else {
                            $.messager.alert("提示", "修改失败，请重新操作！", "info");
                            return;
                        }
                    }
                });
            }
        }
        function removeUser() {
            var test = document.getElementById("test").value = "delete";
            var row = $('#tt').tree('getSelected');
            if (row == null) {
                $.messager.alert("提示", "请选择要删除的数据！", "info");
            }
            
            if (row) {
                if (!$('#tt').tree('isLeaf', row.target)) {
                    $.messager.alert('提示', '你选择没有子元素的数据？', "info");

                }
                else {
                    $.messager.confirm('提示', '你确定要删除这条信息吗？', function (r) {
                        if (r) {
                            $('#fm').form('submit', {
                                url: 'GetPlant.ashx?pid=' + row.id + "&action=" + test,
                                onSubmit: function () {                                      
                                },
                                success: function (result) {
                                    if (result.indexOf("T") == 0) {
                                        $('#dlg').dialog('close');
                                        $.messager.alert("提示", "恭喜您，信息删除成功！", "info");
                                        $('#tt').tree('reload');
                                    }
                                    else {
                                        $.messager.alert("提示", "删除失败，请重新操作！", "info");
                                        return;
                                    }
                                }
                            });
                        }
                    })
                }
            }
        }
        
    </script>

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">    
    <div id="p" class="easyui-panel" title="植物信息"    
         
        data-options="iconCls:'icon-save',tools:'#tab_toolbar'" style="overflow:auto;">   
    <ul id="tt"></ul> 
</div> 
    
 <div id="tab_toolbar" style="padding: 4px; margin-right:10px;">       
     <a href="#" onclick="newNode();" id="a_add" class="icon-add" ></a>
     <a href="javascript:void(0)" onclick="editNode() " class="icon-edit"></a>
     <a href="#" onclick="removeUser();return false;" id="a_del" class="icon-cancel" style="margin-right:10px" ></a>              
    </div>
    <div id="dlg" class="easyui-dialog" style="width: 300px; height: auto; padding: 10px 20px"
        data-options="closed:true,buttons:'#dlg-buttons',shadow:false">
        <form id="fm" method="post">
             <div class="fitem">
                <label>植物编码：</label>
                <input id="pbm" name="pbm" class="easyui-validatebox" data-options="required:true" />                            
            </div>
            <div class="fitem">
                <label>植物名称：</label>
                <input id="pname" name="pname" class="easyui-validatebox" data-options="required:true" />
                <input name="parentid" id="parentid" type="hidden" />   
                <input name="pid" id="pid" type="hidden" /> 
                <input name="Test" id="test" type="hidden" />             
            </div>
        </form>
    </div>
    <div id="dlg-buttons">
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-ok'" onclick="saveNode()">保存</a>
        <a href="#" class="easyui-linkbutton" data-options="iconCls:'icon-cancel'" onclick="javascript:$('#dlg').dialog('close')">关闭</a>
    </div>
    <div id="mm" class="easyui-menu">
         <div onclick="newNode();" data-options="iconCls:'icon-add'">添加</div>
                    <div onclick="editNode() " data-options="iconCls:'icon-edit'">修改</div>
                    <div  onclick="removeUser();return false;" data-options="iconCls:'icon-cancel'" >删除</div>
    </div>
</asp:Content>
<%--<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceCheckright" runat="server">
</asp:Content>--%>
